package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.CoordinateUtils;
import com.ruoyi.system.domain.ElectricFence;
import com.ruoyi.system.mapper.ElectricFenceMapper;
import com.ruoyi.system.service.IElectricFenceService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 电子围栏Service业务层处理
 * 
 * @author wp
 * @date 2023-08-10
 */
@Service
public class ElectricFenceServiceImpl extends ServiceImpl<ElectricFenceMapper, ElectricFence> implements IElectricFenceService {

    @Autowired
    private ElectricFenceMapper electricFenceMapper;

    @DataScope
    @Override
    public List<ElectricFence> selectList(ElectricFence electricFence) {
        return electricFenceMapper.list(electricFence);
    }

    public boolean saveOrUpdateElectricFence(ElectricFence electricFence){
        if (ObjectUtils.isNotEmpty(electricFence.getXyWgs84())){
            //使用的WGS84坐标，转换一份GCJ02坐标
            electricFence.setXyGcj02(electricFence.getXyWgs84().stream().map(CoordinateUtils::transformWGS84ToGCJ02).collect(Collectors.toList()));
        }else if (ObjectUtils.isNotEmpty(electricFence.getXyGcj02())){
            //使用的GCJ02坐标，转换一份WGS84坐标
            electricFence.setXyWgs84(electricFence.getXyGcj02().stream().map(CoordinateUtils::transformGCJ02ToWGS84).collect(Collectors.toList()));
        }else throw new ServiceException("经纬度不能为空！");
        return saveOrUpdate(electricFence);
    }
}
